package cn.didi.feidi.androidorder.assembly;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.databinding.DialogSelectPayWayBinding;

/**
 * Created by jinxiong on 2017/8/29.
 */

public class SelectPayWayFragmentDialog extends BaseDialogFragment {

    private static final String TAG = "SelectPayWayFragmentDia";
    DialogSelectPayWayBinding binding;
    View.OnClickListener aliPayListener;
    View.OnClickListener walletListener;
    View.OnClickListener closeListener;
    boolean tipVisible;
    boolean noCloseActivity;
    CloseActivityCallback mCloseActivityCallback;

    public void setCloseActivityCallback(CloseActivityCallback closeActivityCallback) {
        mCloseActivityCallback = closeActivityCallback;
    }

    public SelectPayWayFragmentDialog setCloseListener(View.OnClickListener closeListener) {
        this.closeListener = closeListener;
        return this;
    }

    @NonNull
    @Override
    protected Dialog newDialog() {

        Dialog dialog = new Dialog(getActivity(), R.style.style_dialog);
        dialog.setCanceledOnTouchOutside(false);
        binding = DataBindingUtil.inflate(getActivity().getLayoutInflater(), R.layout.dialog_select_pay_way,
                (ViewGroup) getActivity().getWindow().getDecorView(), false);
        dialog.setContentView(binding.getRoot());
        setLeftPadding(0).setRightPadding(0).setDialogGravity(Gravity.BOTTOM);
        return dialog;
    }

    @Override
    protected void addListener() {

        binding.dialogSelectPayWayAliTv.setOnClickListener(aliPayListener);
        binding.dialogSelectPayWayWalletTv.setOnClickListener(walletListener);


        if (closeListener != null) {
            binding.dialogSelectPayWayCloseFl.setOnClickListener(closeListener);
        } else {

            binding.dialogSelectPayWayCloseFl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }

    }

    @Override
    protected void initView() {
        Double amount = getArguments().getDouble("amount");

        Log.d(TAG, "initView: " + amount);
        binding.setAmount(amount);
        if (tipVisible) {
            binding.dialogSelectPayWayTipTv.setVisibility(View.VISIBLE);
        }
    }

    public SelectPayWayFragmentDialog setAliPayListener(View.OnClickListener aliPayListener) {
        this.aliPayListener = aliPayListener;
        return this;
    }

    public SelectPayWayFragmentDialog setWalletListener(View.OnClickListener walletListener) {
        this.walletListener = walletListener;
        return this;
    }

    public SelectPayWayFragmentDialog setTipVisible() {
        tipVisible = true;
        return this;
    }

    @Override
    public void dismiss() {
        noCloseActivity = true;
        super.dismiss();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (noCloseActivity) {
            return;
        }
        if (mCloseActivityCallback != null) {
            mCloseActivityCallback.closeCallback();
        }
    }

    public interface CloseActivityCallback {
        void closeCallback();
    }
}
